package com.eventos;

import java.util.List;

import com.eventssmartphone.R;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
//import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ActivityCategoriasEventos extends Activity implements OnItemClickListener, OnClickListener{
	ListView lista;
	ImageButton buscar;
	EditText paramBuscar;
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ev_listaeventos);
		
		lista = (ListView) findViewById(R.id.listaEventos);
		
		List<CatEventoBean> listaCategorias = GestorCatEvento.getInstance().dameCategorias();
		CatEventoAdapter adapter = new CatEventoAdapter(listaCategorias, this);
		
		lista.setAdapter(adapter);
		lista.setOnItemClickListener(this);
		
        buscar = (ImageButton)findViewById(R.id.buscarEventos);        
        buscar.setOnClickListener(this);
        
        paramBuscar = (EditText)findViewById(R.id.searchBar);
	}
	
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		CatEventoBean cat = (CatEventoBean)adapter.getItemAtPosition(position);
		//Toast.makeText(this, "Se seleccionó: " + cat.getIdCategoria()+ " "+ cat.getCategoria(), Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(ActivityCategoriasEventos.this, ActivityTipoEvento.class);
		Bundle b = new Bundle();
		b.putInt("idCategoria", cat.getIdCategoria()); 
		intent.putExtras(b); 
		startActivity(intent);
		
		
	}
	
		
	public void onClick(View v) {
		
		if (v.getId() == R.id.buscarEventos){
			if (filtrar(paramBuscar.getText().toString())){
				if (paramBuscar.getText().toString()!=null && paramBuscar.getText().toString().trim().length() != 0){
					Intent intent = new Intent(this, ActivityListadoSearchEventos.class);
					Bundle b = new Bundle();
					b.putString("parametro", paramBuscar.getText().toString()); 
					intent.putExtras(b); 
					startActivity(intent);
				}	
			} else Toast.makeText(this, "Caracter inválido.", Toast.LENGTH_SHORT).show();
		}
	}
	
	public boolean filtrar (String str){
		boolean valor =true;
		if (str.indexOf("/")!=-1) valor=false;
		if (str.indexOf("#")!=-1) valor=false;
		if (str.indexOf(";")!=-1) valor=false;
		if (str.indexOf(":")!=-1) valor=false;
		if (str.indexOf("?")!=-1) valor=false;
		if (str.indexOf("%")!=-1) valor=false;
		if (str.indexOf("\"")!=-1) valor=false;
		return valor;
	}
}
